﻿using FlaUI.Core.AutomationElements;
using FlaUI.UIA2;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Label = FlaUI.Core.AutomationElements.Label;
using TextBox = FlaUI.Core.AutomationElements.TextBox;

namespace WinFormSee
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }
        System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
        TreeView treeView1 = new TreeView();


        private void Form1_Load(object sender, EventArgs e)
        {
            Panel panel = new Panel();
            panel.Dock = DockStyle.Top;
            this.Controls.Add(panel);
            textBox.Width = 300;
            panel.Controls.Add(textBox);

            System.Windows.Forms.Button button = new System.Windows.Forms.Button();
            button.Text = "查找";
            button.Left = textBox.Width;
            panel.Controls.Add(button);
            panel.Height = textBox.Height;
            treeView1.Dock = DockStyle.Fill;
            //panel.SendToBack();
            treeView1.SendToBack();
            //panel.BringToFront();
            button.Click += Button_Click;

            this.Controls.Add(treeView1);

            
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add("aaa");
            Process[] processes = Process.GetProcessesByName(textBox.Text);
            foreach (var item in processes)
            {
                TreeNode processNode = new TreeNode();
                processNode.Text = item.ProcessName;
                var app = FlaUI.Core.Application.Attach(item.Id);
                using (var automation = new UIA2Automation())
                {
                    Window[] ws = app.GetAllTopLevelWindows(automation);


                    foreach (Window w in ws)
                    {
                        TreeNode wNode = new TreeNode();
                        wNode.Text = w.ToString();
                        LoadElements(w.FindAllChildren(), ref wNode);
                        processNode.Nodes.Add(wNode);
                    }
                    Window window = app.GetMainWindow(automation);

                    TreeNode wNode1 = new TreeNode();
                    wNode1.Text = window.ToString();
                    LoadElements(window.FindAllChildren(), ref wNode1);
                    processNode.Nodes.Add(wNode1);

                    //AutomationElement[] automationElements = window.FindAllChildren();
                    //LoadElements(automationElements, ref processNode);
                    treeView1.Nodes.Add(processNode);
                }
            }
        }

        private void LoadElements(AutomationElement[] automationElements, ref TreeNode treeNode)
        {
            foreach (var AutomationElement in automationElements)
            {
                try
                {
                    TreeNode node = new TreeNode();

                    switch (AutomationElement.ClassName.ToLower())
                    {
                        case "tpanel":
                            node.Text = AutomationElement.ToString() + "[" + AutomationElement.ClassName + "]";
                            break;
                        //case "tbutton":
                        //    Button button = AutomationElement.AsButton();
                        //    node.Text = AutomationElement.ToString() + "[" + AutomationElement.ClassName + "]";
                        //    break;
                        case "tdbgrid":
                            Grid grid = AutomationElement.AsGrid();
                            node.Text = AutomationElement.ToString() + "[" + AutomationElement.ClassName + "]";
                            break;
                        //LoadElements(grid.ColumnHeaders, ref node);
                        case "tbutton":
                        case "tedit":
                            TextBox textBox = AutomationElement.AsTextBox();
                            node.Text = AutomationElement.ToString() + "[" + AutomationElement.ClassName + "]"+"["+textBox.Text+"]";
                            break;
                        default:
                            node.Text = AutomationElement.ToString() + "[" + AutomationElement.ClassName + "]";
                            break;
                    }

                    LoadElements(AutomationElement.FindAllChildren(), ref node);
                    treeNode.Nodes.Add(node);
                }
                catch (Exception ex)
                {
                    Label label = AutomationElement.AsLabel();
                    TreeNode node = new TreeNode();
                    node.Text = AutomationElement.ToString();
                    LoadElements(AutomationElement.FindAllChildren(), ref node);
                    treeNode.Nodes.Add(node);
                }
            }
        }
    }
}
